<template>
	<view>
		<!--<import src="/templates/login/index.wxml"></import>-->
		<view>
			<!-- True -->
			<view v-if="isShow">
				<!-- 检索框 -->
				<search url="/pages/card/search/search"></search>
				<!-- 广告轮播 -->
				<swiper-ad :list="adList"></swiper-ad>
				<!-- 表单 -->
				<tabs @setSwipterData="setSwipterData" :tabList="tabList"></tabs>
				<swiper v-if="tabItems.length > 0" indicator-dots indicator-color indicator-active-color="#19a918" style="height: 350rpx;">
					<block v-for="(item, index) in tabItems" :key="index">
						<swiper-item>
							<tab :tabItem="item"></tab>
						</swiper-item>
					</block>
				</swiper>
				<!-- sticky -->
				<sticky :sticky="sticky" @switch="switchFun"></sticky>
				<!-- 列表 -->
				<view v-for="(item, index) in contents.records" :key="index" v-if="contents.total > 0" class="list" @tap="goDetail"
				 :data-id="item.id">
					<view class="list-title">{{item.title}}</view>
					<view class="list-claim">详情：{{item.claim}}</view>
					<view class="list-date">发布时间：{{item.gmtModified}}</view>
				</view>
				<view v-if="contents.total == 0">
					<view class="list">
						<view class="list-title">暂时没有数据</view>
						<view class="list-company">详情：None</view>
						<view class="list-date">发布时间：None</view>
					</view>
				</view>
				<!-- 浮动按钮 -->
				<float @tap="tapname" v-if="addShow"></float>
				<image src="/static/images/top.jpg" class="go-top" :hidden="!floorstatus" @tap="goTop"></image>
			</view>
			<!-- False -->
			<view v-if="!isShow">
				<!-- 检索框 -->
				<search url="/pages/card/search/search"></search>
				<!-- 广告轮播 -->
				<swiper-ad :list="adList"></swiper-ad>
				<!-- 表单 -->
				<tabs @setSwipterData="setSwipterData" :tabList="ftabItems"></tabs>
				<!-- sticky -->
				<sticky :sticky="fsticky" @switch="switchFun"></sticky>
				<!-- 列表 -->
				<view v-for="(item, index) in fcontents.records" :key="index" v-if="fcontents.total > 0" class="list" @tap="fgoDetail"
				 :data-id="item.id">
					<view class="list-title">{{item.title}}</view>
					<view class="list-claim">详情：{{item.claim}}</view>
					<view class="list-date">发布时间：{{item.gmtModified}}</view>
				</view>
				<view v-if="fcontents.total == 0">
					<view class="list">
						<view class="list-title">暂时没有数据</view>
						<view class="list-company">详情：None</view>
						<view class="list-date">发布时间：None</view>
					</view>
				</view>
				<!-- 浮动按钮 -->
				<image src="/static/images/top.jpg" class="go-top" :hidden="!floorstatus" @tap="goTop"></image>
				<float url="/pages/info/falsePage/falsePage" v-if="addShow"></float>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import search from "../../../components/search/index";
	import swiperAd from "../../../components/swiper-ad/swiper-ad";
	import tabs from "../../../components/tabs/tabs";
	import tab from "../../../components/tab/tab";
	import list from "../../../components/list/list";
	import sticky from "../../../components/sticky/sticky";
	import float from "../../../components/float/float";

	export default {
		data() {
			return {
				fromData: {
					openid: "",
					nickname: "",
					sex: "",
					province: "",
					city: "",
					country: "",
					headimgurl: "",
					privilege: "",
					unionid: ""
				},
				url: "",
				adList: [],
				tabItems: [],
				ftabItems: [
					["论坛热门", "", true],
					["论坛笑话", "", false]
				],
				contents: [],
				fcontents: [],
				sticky: {
					"left": "热门名片",
					"right": "最新发布的名片"
				},
				fsticky: {
					"left": "热门帖子",
					"right": "最新发布"
				},
				addShow: true,
				isShow: false,
				typeOfWork: 1,
				floorstatus: false,
				tabList: [
					["招工", "", true],
					["找活", "", false]
				]
			};
		},

		components: {
			search,
			swiperAd,
			tabs,
			tab,
			list,
			sticky,
			float
		},

		onLoad(option) {
			let _this = this;
			var url = window.location.href //获取url中"?"符后的字串
			console.log('url', url)
			uni.setStorageSync('url', url),
				this.setData({
					url: url
				});
			uni.request({
				url: `${app.globalData.baseUrl}/H5/Oauth`,
				method: "GET",
				dataType: "json",
				data: {
					url: url
				},
				success: res => {
					console.log("openid", res.data.dataMap.data.openid),
						uni.setStorageSync("openid", res.data.dataMap.data.openid)
					uni.request({
						url: `${app.globalData.baseUrl}/H5/getUserInfo/${uni.getStorageSync("openid")}`,
						method: "GET",
						success: res => {
							console.log("userInfo1", res.data.dataMap.data),
								uni.setStorageSync("userInfo", res.data.dataMap.data)
							this.setData({
								fromData: res.data.dataMap.data
							});
							uni.request({
								url: `${app.globalData.baseUrl}/H5/getInfo/${uni.getStorageSync("openid")}`,
								method: "GET",
								success: res => {
									// console.log("userInfo", res.data.dataMap.data),
									if (res.data.dataMap.data === null) {
										console.log("headimgurl", uni.getStorageSync("userInfo").headimgurl)
										console.log("nickname", uni.getStorageSync("userInfo").nickname)
										uni.request({
											url: `${app.globalData.baseUrl}/user/H5/register/${uni.getStorageSync("openid")}`,
											method: "POST",
											dataType: "json",
											data: this.fromData,
											success: res => {
												console.log("userInfo2", res.data.dataMap.data)
											}
										});
									}
								}
							});
						}
					});
				}
			});
			uni.request({
				url: `${app.globalData.baseUrl}/mallappletbackstage/is-show/select/isShow`,
				success: res => {
					const isShow = res.data.dataMap.data.isShow;

					if (isShow == 1) {
						_this.setData({
							isShow: true
						});
					} else {
						_this.setData({
							isShow: false
						});
					}

					if (this.isShow) {
						// 第二个数据
						let tab = [];
						let _this = this;

						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let contents = _this.contents;
								contents = res.data.dataMap.data;

								_this.setData({
									contents,
									sticky: {
										"left": "热门工作",
										"right": "最新发布的工作"
									}
								});
							},

							fail(res) {
								
							}

						});
						this.setData({
							tabItems: tab,
							addShow: true
						});
					} else {
						uni.setNavigationBarTitle({
							title: '工友论坛' // 导航栏标题

						});
						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let fcontents = _this.contents;
								fcontents = res.data.dataMap.data;

								_this.setData({
									fcontents
								});
							}
						});
					}
				}
			});
		},

		onShow: function() {
			let _this = this;

			uni.request({
				url: `${app.globalData.baseUrl}/H5/getUserInfo/${uni.getStorageSync("openid")}`,
				method: "GET",
				success: res => {
					console.log("userInfo", res.data.dataMap.data),
						uni.setStorageSync("userInfo", res.data.dataMap.data)
				}
			});

			uni.request({
				url: `${app.globalData.baseUrl}/mallappletbackstage/is-show/select/isShow`,
				success: res => {
					const isShow = res.data.dataMap.data.isShow;

					if (isShow == 1) {
						_this.setData({
							isShow: true
						});
					} else {
						_this.setData({
							isShow: false
						});
					}

					if (this.isShow) {
						// 第二个数据
						let tab = [];

						let _this = this;

						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let contents = _this.contents;
								contents = res.data.dataMap.data;

								_this.setData({
									sticky: {
										"left": "热门工作",
										"right": "最新发布的工作"
									}
								});
							}
						});
						this.setData({
							tabItems: tab,
							addShow: true
						});
					} else {
						uni.setNavigationBarTitle({
							title: '工友论坛' // 导航栏标题

						});
						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let contents = _this.contents;
								contents = res.data.dataMap.data.records;

								_this.setData({
									contents
								});
							}
						});
					}
				}
			});
		},

		// 下拉刷新
		onPullDownRefresh() {
			// 第二个数据
			let tab = [];
			uni.showLoading({
				title: '加载中'
			}); //

			let _this = this;

			uni.request({
				url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${_this.typeOfWork}`,
				success: res => {
					let contents = _this.contents;
					contents = res.data.dataMap.data;

					_this.setData({
						contents,
						sticky: {
							"left": "热门工作",
							"right": "最新发布的工作"
						}
					});
				},
				complete: res => {
					uni.stopPullDownRefresh();

					_this.setData({
						tabItems: tab,
						addShow: true
					});
				}
			});
		},

		onShareAppMessage: function(res) {
			if (res.from === 'button') {
				// 来自页面内转发按钮
				console.log(res.target);
			}

			return {
				title: `我的名片`,
				path: '/page/user?id=123'
			};
		},
		onReachBottom: function() {
			if (this.isShow) {
				if (this.contents.current < this.contents.pages) {
					uni.showLoading();
					let contents = this.contents;

					let _this = this;

					uni.request({
						url: `${app.globalData.baseUrl}/release-work/work/${this.contents.current + 1}?typeOfWork=${this.typeOfWork}`,
						complete: res => {
						},
						fail: res => {},
						success: result => {
							contents.current += 1;
							result.data.dataMap.data.records.forEach(item => contents.records.push(item));

							_this.setData({
								contents
							});
						}
					});
				}
			}
		},
		methods: {
			login() {
				window.location.href =
					"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx022815589dbefd3e&redirect_uri=http://we7.91gongju.cn/api.php?id=1&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
			},
			goTop: function(e) {
				if (uni.pageScrollTo) {
					uni.pageScrollTo({
						scrollTop: 0
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
					});
				}
			},
			onPageScroll: function(e) {
				if (e.scrollTop > 100) {
					this.setData({
						floorstatus: true
					});
				} else {
					this.setData({
						floorstatus: false
					});
				}
			},

			setSwipterData(e) {
				const current = e.detail;


				if (this.isShow) {
					if (current == 0) {
						this.setData({
							typeOfWork: 1
						}); // 第二个数据

						let tab = [];
						

						let _this = this;

						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let contents = _this.contents;
								contents = res.data.dataMap.data;

								_this.setData({
									contents,
									sticky: {
										"left": "热门工作",
										"right": "最新发布的工作"
									}
								});
							}
						});
						this.setData({
							tabItems: tab,
							addShow: true
						});
					}
					if (current == 1) {
						this.setData({
							typeOfWork: 0
						}); // 第三个

						let tab = [];

						let _this = this;

						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let contents = _this.contents;
								contents = res.data.dataMap.data;

								_this.setData({
									contents,
									sticky: {
										"left": "热门工作",
										"right": "最新发布的工作"
									}
								});
							}
						});
						this.setData({
							tabItems: tab,
							addShow: true
						});
					}
				} else {
					let _this = this;

					if (current == 0) {
						this.setData({
							typeOfWork: 1
						});
						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let fcontents = _this.contents;
								fcontents = res.data.dataMap.data;
								_this.setData({
									fcontents: fcontents
								});
							}
						});
					}
					if (current == 1) {
						this.setData({
							typeOfWork: 0
						});
						uni.request({
							url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
							success: res => {
								let fcontents = _this.contents;
								fcontents = res.data.dataMap.data;

								_this.setData({
									fcontents: fcontents
								});
							}
						});
					}
				}
			},

			goDetail: function(e) {
				let id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: `/pages/common/detail/detail?id=${id}`
				});
			},
			fgoDetail: function(e) {
				let id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: `/pages/common/detail/fdetail/fdetail?id=${id}`
				});
			},

			switchFun(e) {
				// 第二个数据
				let tab = [];
				

				let _this = this;

				uni.request({
					url: `${app.globalData.baseUrl}/release-work/work/${1}?typeOfWork=${this.typeOfWork}`,
					success: res => {
						let contents = res.data.dataMap.data;
						contents.records = contents.records.sort((item1, item2) => item2.gmtModified.localeCompare(item1.gmtModified));

						_this.setData({
							contents
						});
					},
					complete: res => {
					}
				});
				this.setData({
					tabItems: tab,
					addShow: true
				});
			},

			onShareTimeline() {}

		}
	};
</script>
<style>
	.list {
		width: 95%;
		height: 200rpx;
		margin: 2.5%;
		border-bottom: solid 1px #e6e6ee;
		background: #fff;
		padding: 5rpx 10rpx;
	}

	.list-title {
		font-size: 40rpx;
		margin-top: 20rpx;
	}

	.list-date {
		margin-top: 10rpx;
	}

	.list-claim {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	page {
		background: #efeff5;
	}

	.go-top {
		right: 28rpx;
		width: 110rpx;
		height: 110rpx;
		position: fixed;
		bottom: 400rpx;
		border-radius: 50%;
	}
</style>
